package com.Cychat.post.mapper;


import com.Cychat.commons.entity.pojo.Comment;
import com.Cychat.post.entity.dto.CommentDTO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Auther: 崔航
 * @Date: 2025/10/28:14:24
 * @Description:
 */
@Mapper
public interface CommentMapper extends BaseMapper<Comment> {
    @Select("SELECT * FROM comment c \n" +
            "JOIN user u ON c.user_id = u.user_id \n" +
            "WHERE c.post_id = #{postId} \n" +
            "  AND c.status = 1 \n" +
            "  AND c.level = 1\n" +
            "ORDER BY c.like_count DESC")
    public List<Comment> getCommentsByPostId(Integer postId);

    @Select("SELECT *, " +
            "CASE WHEN c.parent_id != c.root_id THEN pu.nick_name ELSE NULL END as replyTo " +
            "FROM comment c " +
            "JOIN user u ON c.user_id = u.user_id " +
            "LEFT JOIN comment pc ON c.parent_id = pc.comment_id AND c.parent_id != c.root_id " +
            "LEFT JOIN user pu ON pc.user_id = pu.user_id " +
            "WHERE c.root_id = #{rootId} AND c.status = 1 " +
            "ORDER BY c.like_count DESC")
    public List<CommentDTO> getCommentsByRootId(Integer rootId);

    @Select("SELECT * FROM comment WHERE comment_id = #{commentId}")
    Comment getCommentById(Integer commentId);
}
